package com.meituan.controller;

import com.meituan.pojo.req.UserReq;
import com.meituan.pojo.req.UserRoleReq;
import com.meituan.pojo.resp.BaseResp;
import com.meituan.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    //邮件发送接口
    @PostMapping("/sendMail")
    public BaseResp sendMail(@RequestBody UserReq userReq, HttpSession session){
        return userService.sendMail(userReq.getEmail(),session);
    }

    //注册接口
    @PostMapping("/regist")
    public BaseResp regist(@RequestBody UserReq UserReq, HttpSession session){
        return userService.regist(UserReq,session);
    }

    //登录接口
    @PostMapping("/login")
    public BaseResp login(@RequestBody UserReq UserReq,HttpSession httpSession){
        return userService.login(UserReq);
    }

    //校验接口
    @PostMapping("/isAuthcation")
    public BaseResp isAuthcation(){
        Subject subject = SecurityUtils.getSubject();
        boolean authenticated = subject.isAuthenticated();
        if (authenticated){
            return new BaseResp(1,"success",subject.getPrincipal());
        }
        return new BaseResp().ERROR();
    }

    //注销接口
    @PostMapping("logout")
    public BaseResp logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return new BaseResp().SUCCESS();
    }

//    @RequiresPermissions(value ={"user_findAll"})
    @PostMapping("findAll")
    public BaseResp findAll(){
        return userService.findAll();
    }

//    @RequiresPermissions(value ={"user_delete"})
    @PostMapping("deleteById")
    public BaseResp deleteById(@RequestBody Map map){
        return userService.deleteById(Integer.parseInt(map.get("id").toString()));
    }

//    @RequiresPermissions(value ={"user_update"})
    @PostMapping("/saveOrUpdate")
    public BaseResp saveOrUpdate(@RequestBody UserReq userReq){
        System.out.println(userReq);
        return userService.saveOrUpdate(userReq);
    }

//    @RequiresPermissions(value ={"user_findAll"})
    @PostMapping("/findRoleByUid")
    public BaseResp findRoleByUid(@RequestBody Map map){
        return userService.findRoleByUid(Integer.parseInt(map.get("id").toString()));
    }

//    @RequiresPermissions(value ={"user_findAll"})
    @PostMapping("/updateUserRole")
    public BaseResp updateUserRole(@RequestBody UserRoleReq userRoleReq ){
        return userService.updateUserRole(userRoleReq);
    }
}
